#include <stdio.h>
#include <string.h>
#include <getch.h>
#include <stdlib.h>

#include "initProgram.h"
#include "tchFunc.h"
#include "tchManage.h"
#include "tools.h"

void tchView(void)	//教师操作界面
{
	system("clear");
	printf(" _________________________\n");
	printf("|                         |\n");
	printf("|--------欢迎登录---------|\n");
	printf("|----输入需要执行的功能---|\n");
	printf("|-----1.添加学生----------|\n");
	printf("|-----2.删除学生----------|\n");
	printf("|-----3.修改学生----------|\n");
	printf("|-----4.查询学生----------|\n");
	printf("|-----5.添加成绩----------|\n");
	printf("|-----6.重置学生密码------|\n");
	printf("|-----7.显示在校学生------|\n");
	printf("|-----8.显示离校学生------|\n");
	printf("|-----9.退出登陆----------|\n");
	printf("|_________________________|\n");
}

void tchAddStuView(void) //添加学生界面
{
	printf("1、单次添加\n");
	printf("2、从文件中导入\n");
	printf("请选择功能: \n");
		switch(getch())
		{
			case '1': 
			{		
				Student stu = {}; //存储新添加的信息
				printf("请按序输入学生信息:\n");
				printf("姓名 性别 语文成绩 数学成绩 英语成绩\n");
				scanf("%s %c %lf %lf %lf", stu.name, &stu.sex, &stu.chnScore, &stu.mathScore, &stu.engScore);
				for(int i = 0; i < STUMAX; i++) //遍历寻找空位置
				{
					if(student[i].id == 0) //有空位置
					{
						stu.id = i+1001; //生成学生学号
						tchAddStuOnce(stu);
						printf("添加成功！\n");
						STUNUM++;
						anykey_continue();
						break;
					}
					else if(i == STUMAX-1) //遍历至最后一位，位置已满
					{
						printf("学生列表已满，添加失败！\n");
						anykey_continue();
					}
				}
			}	break;
			case '2': 
			{	
				char filePath[100] = {}; //文件路径
				printf("请输入文件路径：\n");
				scanf("%s",filePath);
				tchAddStuByFile(filePath);
			}	break;

		}
}

void tchAddStuOnce(Student stu)
{
	int i = stu.id - 1001; //根据学号定位下标
	char password[20] = {};
	sprintf(password,"%d",stu.id);
	strcpy(student[i].name, stu.name);
	strcpy(student[i].password, password);
	student[i].chnScore = stu.chnScore;
	student[i].mathScore = stu.mathScore;
	student[i].engScore = stu.engScore;
	student[i].id = stu.id;
	student[i].sex = stu.sex;
}

int tchAddStuByFile(char* fileName)
{
	FILE* frp = fopen(fileName,"r");
	if(NULL == frp)
	{
		perror("fopen");
		return -1;
	}
	int successTimes = 0;
	int failedTimes = 0;
	char str[256] = {};
	Student stu = {};
	while(fgets(str,sizeof(Student),frp))
	{
		sscanf(str, "%s %c %lf %lf %lf", stu.name, &stu.sex, &stu.chnScore, &stu.mathScore, &stu.engScore);
		for(int i = 0; i < STUMAX; i++)
		{
			if(0 == student[i].id)
			{
				student[i].id = i + 1001;
				strcpy(student[i].name, stu.name);
				sprintf(student[i].password,"%d",student[i].id);
				student[i].chnScore = stu.chnScore;
				student[i].mathScore = stu.mathScore;
				student[i].engScore = stu.engScore;
				student[i].sex = stu.sex;
				student[i].flag = stu.flag;
				STUNUM++;
				successTimes++;
				break;
			}
			if(i == STUMAX-1)
			{
				failedTimes++;
			}
		}
	}
	printf("添加成功%d次\n",successTimes);
	printf("添加失败%d次\n",failedTimes);
	anykey_continue();
	fclose(frp);
	frp = NULL;
}

void tchDeleteStu(int id)
{
	Student stu = {};
	student[id-1001] = stu;
	printf("删除成功！\n");
	STUNUM--;
	anykey_continue();
}

void tchModifyStuView(void)
{
	printf("1、修改学生基本信息\n");
	printf("2、修改学生成绩信息\n");
	printf("3、修改学生帐号状态\n");
	printf("请选择功能: \n");	
		switch(getch())
		{
			case '1': 
			{	
				int stuId = 0;
				printf("请输入学生学号：\n");
				scanf("%d",&stuId);
				while(stuId < 1001 || stuId > 2000 || 0 == student[stuId-1001].id)
				{
					printf("学号有误或不合法，请重新输入:\n");
					scanf("%d",&stuId);
				}
				tchModifyStuBasicInfo(stuId - 1001);
				anykey_continue();
			}	break;

			case '2':
			{
				int stuId = 0;
				printf("请输入学生学号：\n");
				scanf("%d",&stuId);
				while(stuId < 1001 || stuId > 2000 || 0 == student[stuId-1001].id)
				{
					printf("学号有误或不合法，请重新输入:\n");
					scanf("%d",&stuId);
				}
				tchModifyStuScore(stuId - 1001);
				anykey_continue();
			}	break;	
			case '3':
			{
				int stuId = 0;
				int newFlag = 0;
				printf("请输入学生学号：\n");
				scanf("%d",&stuId);
				while(stuId < 1001 || stuId > 2000 || 0 == student[stuId-1001].id)
				{
					printf("学号有误或不合法，请重新输入:\n");
					scanf("%d",&stuId);
				}
				system("clear");
				printf("学号\t帐号状态\n");
				printf("%d\t%s\n",student[stuId-1001].id,student[stuId-1001].flag==0?"初始化":student[stuId-1001].flag==1?"正常":student[stuId].flag==2?"已离校":"已锁定");
				printf("请输入新的帐号状态(1:正常,2:离校,-1:锁定中)\n");
				scanf("%d",&newFlag);
				while(student[stuId-1001].flag == newFlag || -1 > newFlag || 2 < newFlag || 0 == newFlag)
				{
					printf("输入有误或与原状态重复，请重新输入:\n");
					scanf("%d",&newFlag);
				}
				student[stuId-1001].flag = newFlag;
				printf("修改成功！\n");
				anykey_continue();
			}	break;
		}
}

void tchModifyStuBasicInfo(int num)
{
	printf("学号\t姓名\t性别\n");
	printf("%d\t%s\t%s\n", student[num].id, student[num].name, student[num].sex=='w'?"女":"男");
	printf("请输入要修改成的姓名、性别\n");
	scanf("%s %c",student[num].name,&student[num].sex);
	printf("修改成功！\n");
}

void tchModifyStuScore(int num)
{
	printf("学号\t姓名\t语文\t数学\t英语\n");
	printf("%d\t%s\t%.1f\t%.1f\t%.1f\n", student[num].id, student[num].name, student[num].chnScore, student[num].mathScore,student[num].engScore);
	printf("请输入要修改成的语文成绩、数学成绩、英语成绩\n");
	scanf("%lf %lf %lf",&student[num].chnScore,&student[num].mathScore,&student[num].engScore);
	printf("修改成功！\n");
}

void tchSearchStuView(void)
{
	printf("1、通过学号查询\n");
	printf("2、通过姓名查询\n");
	printf("请选择功能: \n");	
		switch(getch())
		{
			case '1':
			{
				int id = 0;
				printf("请输入学号:\n");
				scanf("%d", &id);
				tchSearchStuByid(id);
			}	break;
			case '2':
			{
				char name[20] = {};
				printf("请输入姓名:\n");
				scanf("%s", name);
				tchSearchStuByName(name);	
			}	break;
		}
}

void tchSearchStuByid(int id)
{
	for(int i = 0; i < STUMAX; i++)
	{
		if(id == student[i].id)
		{
			printf("学号\t姓名\t性别\t语文\t数学\t英语\t帐号状态\n");
			printf("%d\t%s\t%s\t%.1lf\t%.1lf\t%.1lf\t%s\n",student[i].id,student[i].name,student[i].sex=='w'?"女":"男",student[i].chnScore,student[i].mathScore,student[i].engScore,student[i].flag==0?"初始化":student[i].flag==1?"正常":student[i].flag==2?"已离校":"已锁定");
			anykey_continue();
			break;
		}
		if(i == STUMAX-1)
		{
			printf("未找到该学生\n");
			anykey_continue();
			break;
		}
	}
}

void tchSearchStuByName(char* name)
{
	for(int i = 0; i < STUMAX; i++)
	{
		if(0 == strcmp(name,student[i].name))
		{
			printf("学号\t姓名\t性别\t语文\t数学\t英语\t帐号状态\n");
			printf("%d\t%s\t%s\t%.1lf\t%.1lf\t%.1lf\t%s\n",student[i].id,student[i].name,student[i].sex=='w'?"女":"男",student[i].chnScore,student[i].mathScore,student[i].engScore,student[i].flag==0?"初始化":student[i].flag==1?"正常":student[i].flag==2?"已离校":"已锁定");
			anykey_continue();
			break;
		}
		if(i == STUMAX-1)
		{
			printf("未找到该学生\n");
			anykey_continue();
			break;
		}
	}
}

void tchAddScoreView(void)
{	
	printf("1、单次添加\n");
	printf("2、从文件中导入\n");
	printf("请选择功能: \n");
		switch(getch())
		{
			case '1':
			{	
				int id = 0;
				double chnScore = 0, mathScore = 0, engScore = 0;
				printf("请输入学生学号:\n");
				scanf("%d", &id);
				while(id > 2000 || id < 1001 || 0 == student[id-1001].id)
				{
					printf("学号不合法！请重新输入:\n");
					scanf("%d",&id);
					getchar();
				}
				printf("请输入语文成绩、数学成绩、英语成绩:\n");
				scanf("%lf %lf %lf", &chnScore, &mathScore, &engScore);
				tchAddScoreOnce(id,chnScore,mathScore,engScore);
				show_msg("修改成功！",1.5);
			}	break;
			case '2':
			{	
				int failedTimes = 0;
				int updateTimes = 0;
				char filePath[100] = {};
				printf("请输入文件路径:\n");
				scanf("%s",filePath);
				int ret = tchAddScoreByFile(filePath,&failedTimes);
				-1 == ret && printf("文件路径错误\n");
				0 < ret && printf("添加成功%d次\n",ret);
				printf("添加失败%d次\n",failedTimes);
				anykey_continue();
			}	break;
	}
}

void tchAddScoreOnce(int id, double chnScore, double mathScore, double engScore)
{
	int i = id - 1001;
	student[i].chnScore = chnScore;
	student[i].mathScore = mathScore;
	student[i].engScore = engScore;
}

int tchAddScoreByFile(char* fileName, int* failedTimes)
{
	FILE* frp = fopen(fileName,"r");
	if(NULL == frp)
	{
		perror("fopen");
		return -1;
	}
	char str[256] = {};
	Student stu = {};
	int successTimes = 0;
	while(fgets(str,sizeof(Student),frp))
	{
		sscanf(str, "%d %lf %lf %lf", &stu.id, &stu.chnScore, &stu.mathScore, &stu.engScore);
		int i = stu.id - 1001;
		if(stu.id > 1000 && stu.id < 2001 && student[i].id)
		{
			student[i].id = stu.id;
			student[i].chnScore = stu.chnScore;
			student[i].mathScore = stu.mathScore;
			student[i].engScore = stu.engScore;
			successTimes++;
		}
		else
		{
			(*failedTimes)++;
		}
	}
	fclose(frp);
	frp = NULL;
	return successTimes;
}

void tchResetStuPassword(int id)
{
	student[id-1001].flag = 0;
	sprintf(student[id-1001].password, "%d", id);
}

void tchViewStuInfo(void)
{
	system("clear");
	printf("学号\t姓名\t性别\t语文\t数学\t英语\t帐号状态\n");
	for(int i = 0; i < STUMAX; i++)
	{
		if(0 != student[i].id && 2 > student[i].flag)
		{
			printf("%d\t%s\t%s\t%.1lf\t%.1lf\t%.1lf\t%s\n",student[i].id,student[i].name,student[i].sex=='w'?"女":"男",student[i].chnScore,student[i].mathScore,student[i].engScore,student[i].flag==0?"初始化":student[i].flag==1?"正常":"锁定中");
		}
	}
	anykey_continue();
}

void tchViewLeaveStuInfo(void)
{
	system("clear");
	int ifExist = 0;
	printf("学号\t姓名\t性别\t语文\t数学\t英语\n");
	for(int i = 0; i < STUMAX; i++)
	{
		if(0 != student[i].id && 2 == student[i].flag)
		{
			printf("%d\t%s\t%s\t%.1lf\t%.1lf\t%.1lf\n",student[i].id,student[i].name,student[i].sex=='w'?"女":"男",student[i].chnScore,student[i].mathScore,student[i].engScore);
			ifExist++;
		}
	}
	if(!ifExist)
	{
		printf("暂无信息\n");
	}
	anykey_continue();
}
